package com.huitone.gddw.service.impl.worktable;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.huitone.gddw.common.ComConstant;
import com.huitone.gddw.dao.entity.LinkSendPro;
import com.huitone.gddw.dao.worktable.paramconfigmanage.ILinkSendProDao;
import com.huitone.gddw.service.ILinkSendProService;
import com.huitone.gddw.service.IParamtabRecService;
import com.huitone.gddw.utils.StringUtils;
import com.huitone.gddw.utils.SysLoginUtil;

/**
 * 通道可发送省份 接口实现类
 * @author wuluodan
 *
 */
@Service
public class LinkSendProService implements ILinkSendProService {

	@Autowired
	private ILinkSendProDao linkSendProDao;
	
	@Autowired
	private IParamtabRecService paramtabRecService;
	
	@Override
	public List<LinkSendPro> getByGateLinkId(String gateLinkId) {
		return linkSendProDao.getByGateLinkId(gateLinkId);
	}
	
	@Override
	public List<String> getProByGateLinkId(String gateLinkId) {
		List<String> listPro = new ArrayList<String>();
		List<LinkSendPro> list = this.getByGateLinkId(gateLinkId);
		for (LinkSendPro linkSendPro : list) {
			listPro.add(linkSendPro.getProvince());
		}
		return listPro;
	}

	@Override
	public void updateByGateLinkId(String gateLinkId, List<String> listSave, List<String> listDel) {
		List<LinkSendPro> list = new ArrayList<LinkSendPro>();
		for (String province : listSave) {
			LinkSendPro entity = new LinkSendPro();
			entity.setGateLinkId(gateLinkId);
			entity.setProvince(province);
			list.add(entity);
		}
		this.saveBatch(list);
		this.deleteByGateLinkId(gateLinkId, listDel);
	}
	
	/**
	 * 批量保存
	 * @param list
	 */
	private void saveBatch(List<LinkSendPro> list) {
		for (LinkSendPro linkSendPro : list) {
			linkSendPro.setRuleId(StringUtils.uuid());
			linkSendPro.setInsertTime(new Date());
			linkSendPro.setInsertStaffId(SysLoginUtil.getLoginUserId());
			linkSendPro.setLastModTime(linkSendPro.getInsertTime());
			linkSendPro.setLastModStaffId(linkSendPro.getInsertStaffId());
		}
		if (!list.isEmpty() && list.size() > 0) {
			this.updateParamRec("批量保存通道可发送省份");
			linkSendProDao.insertBatch(list);
		}
	}
	
	/**
	 * 批量删除通道下指定的可发送省份
	 * @param gateLinkId 通道ID
	 * @param listDel 省份
	 */
	private void deleteByGateLinkId(String gateLinkId, List<String> listDel) {
		if (!listDel.isEmpty() && listDel.size() > 0) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("gateLinkId", gateLinkId);
			map.put("list", listDel);
			linkSendProDao.deleteByGateLinkId(map);
			this.updateParamRec("根据通道ID删除指定的通道可发送省份");
		}
	}
	
	/**
	 * 更新参数记录表
	 * @param desc 说明
	 */
	private void updateParamRec(String desc) {
		paramtabRecService.update(ComConstant.LINK_SEND_PRO, desc);
	}

}
